
using System;
using System.Xml;
using Habazie.kf2.Core;
using Habazie.kf2.Net;
using Habazie.kf2.Net.protocol;

namespace Habazie.kf2.GUI
{
	
	
	public class PrivacyListManager : Gtk.Dialog
	{
		private Account m_account;
		
		protected Gtk.TreeView m_list;
		protected Gtk.Button m_add;
		protected Gtk.Button m_edit;
		protected Gtk.Button m_delete;
		protected Gtk.ToggleButton m_active;
		protected Gtk.ToggleButton m_default;
		protected Gtk.VButtonBox m_buttons;
		
		private Gtk.ListStore m_store;


		
		public PrivacyListManager(Account acc)
		{
			m_account = acc;
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.PrivacyListManager));
			
			m_active = new Gtk.ToggleButton ("Active");
			m_buttons.PackStart (m_active, false, false, 6);
			m_active.Show ();
			
			m_default = new Gtk.ToggleButton ("Default");
			m_buttons.PackStart (m_default, false, false, 0);
			m_default.Show ();
			
			m_store = new Gtk.ListStore (typeof (string));
			m_list.Model = m_store;
			m_list.AppendColumn ("Lists", new Gtk.CellRendererText (),
					"text", 0);
			
			FetchLists ();
		}
		
		
		private void SetSensitive (bool i)
		{
			this.VBox.Sensitive = i;
		}
		
		// Network stuff
		protected void FetchLists ()
		{
			PrivacyIQ iq = new PrivacyIQ (m_account.Document);
			iq.Type = jabber.protocol.client.IQType.get;
			m_account.BeginWrite (iq, new jabber.protocol.ProtocolHandler (ListsFetched));
		}
		
		protected void ListsFetched (object o, System.Xml.XmlElement e)
		{
			Util.Debug ("gui", "jabber:iq:privacy: got response: {0}", e);
			
			m_store.Clear ();
			
			jabber.protocol.client.IQ iq = e as jabber.protocol.client.IQ;
			if (iq == null)
				return;
				
			foreach (XmlNode node in iq.Query) {
				PrivacyList list = node as PrivacyList;
				if (list != null) {
					m_store.AppendValues (list.ListName);
				}
			}
		}
		
		
		// Event handlers

		protected virtual void OnCloseClicked(object sender, System.EventArgs e)
		{
			Destroy ();
		}
		
		
		protected virtual void OnMAddClicked(object sender, System.EventArgs e)
		{
		}

		protected virtual void OnMEditClicked(object sender, System.EventArgs e)
		{
		}

		protected virtual void OnMDeleteClicked(object sender, System.EventArgs e)
		{
		}
	}
}
